
// const apiUrl = 'https://fat-app-api.blueshieldfintech.com'; // test url
const apiUrl = 'https://dc-api.rupee-mama.in'; // prod url

let messagesArray = [
//     {
//         customerType: 1,
//         replyDate: "2020-04-01 12:00:00",
//         replyContent: "Hello, what can I do for you?",
//         customerImg: "www.google.com/for/img1"
//    }
]
function populateMessages(msgData) {
    const chatBox = document.querySelector('section .chat-box ul')
    chatBox.innerHTML = ''
    msgData.forEach(d => {
        chatBox.insertAdjacentHTML('afterbegin',`
        <li class="${d.customerType ? 'server-message': 'client-message'}">
            <p class="replyDate">${d.replyDate}</p>
            ${d.replyContent}
        </li>
        `)
    })
    document.querySelector("section .chat-box").scrollTo({
        left: 0,
        top: document.querySelector("section .chat-box").scrollHeight - window.innerHeight + 90,
        behavior: 'smooth'
    })
}
function sendMessage(ele) {
    if (!(typeof event.key === 'undefined' || event.key === 'Enter')) { return ;}
    const userMessage = document.querySelector('#userMessageInput')
    if (userMessage.value === '') { return ;}
    ajaxSendMessage(userMessage.value)
    userMessage.value = ''
}

function ajaxSendMessage(message) {
    $.ajax({
        url: apiUrl + "/user/feedback/save",
        type: "POST",
        headers: {
            basicParams: decodeURI(getQueryVariable('basicParams'))
        },
        data: JSON.stringify({
            content: message
        }),
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (res) {
            if (res.code == 0) {
                messagesArray.unshift({
                    customerType: 1,
                    replyDate: Date.toString(),
                    replyContent: message,
                    customerImg: "www.google.com/for/img1"
               })
                return ajaxGetMessages()
            }
            alert('Error Please try again later')
        },
        error: function (res) {
            console.log(res);
        }
    })
}

function ajaxGetMessages() {
    $.ajax({
        url: apiUrl + "/user/feedback/list",
        type: "POST",
        headers: {
            basicParams: decodeURI(getQueryVariable('basicParams'))
        },
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (res) {
            if (res.code != 0) {
                alert('Something Went wrong, please try again after sometime')
                return ;
            }
            messagesArray = (res.data.feedbackList).reverse();
            populateMessages(messagesArray)
        },
        error: function (res) {
            console.log(res);
        }
    })
}
ajaxGetMessages()
const getQueryParams = ( params, url ) => {
    let href = url;
    let reg = new RegExp( '[?&]' + params + '=([^&#]*)', 'i' );
    let queryString = reg.exec(href);
    return queryString ? queryString[1] : null;
  };
  function getQueryVariable(variable) {
    let query = window.location.search.substring(1);
    let vars = query.split("&");
    for (let i = 0; i < vars.length; i++) {
        let pair = vars[i].split("=");
        if (pair[0] == variable) {
            console.log(decodeURI(pair[1]));
            return pair[1];
        }
    }
    return (false);
}

// function findSpecialKeywords(text) {
//     const emailReg = new RegExp('\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b')
//     const phoneReg = new RegExp('\b[0-9]*\b')
//     text.replace('\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b', '<a href=""></a>')
// }